package xyz.xuminghai.javafx_learn.scene.control;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * 2023/5/24 14:58 星期三<br/>
 * ToggleButton是一个专门的控件，可以被选中。通常ToggleButton呈现方式与 Button 类似。但是，它们是两种不同类型的控件。
 * Button 是一个“命令”按钮，单击它会调用一个函数。
 * 另一方面， ToggleButton只是一个带有布尔值的控件，指示它是否已被选中
 *
 * @author xuMingHai
 */
public class ToggleButtonApp extends Application {

    private final HBox hBox = new HBox(30.0);

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        ToggleButton toggleButton1 = new ToggleButton("toggleButton1");
        toggleButton1.selectedProperty().addListener((observable, oldValue, newValue) -> System.out.println("selected = " + newValue));

        ToggleButton toggleButton2 = new ToggleButton("toggleButton2");
        ToggleButton toggleButton3 = new ToggleButton("toggleButton3");

        ToggleGroup toggleGroup = new ToggleGroup();
        toggleButton1.setToggleGroup(toggleGroup);
        toggleButton2.setToggleGroup(toggleGroup);
        toggleButton3.setToggleGroup(toggleGroup);


        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(toggleButton1, toggleButton2, toggleButton3);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(hBox, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
